<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/


defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');
/**
* This controller is used for configuration feature of the component
*/
class JobBoardControllerJaresumes extends JobBoardController{

    /**
     * Constructor
     */
    function __construct( $default = array() )
    {
    	 $layout = JRequest::getVar('layout');
        if ($layout=='download')
        	JRequest::setVar('task','download');
//        	var_dump($layout);exit;
        parent::__construct( $default );
        $this->set('name', 'resumes');
        $this->set('fullname', 'jaresumes');
        
        // Register Extra tasks
        $layout = JRequest::getVar('layout');
        if ($layout=='download')
        	
        $this->registerTask( 'add',        'edit' );
        $this->registerTask( 'display',    'view' );
        $this->registerTask( 'inactivate',    'activate' );
		
    }
    
    /**
    * Display current configuration of the component to administrator
    * 
    */
    function display(){
    	$layout = JRequest::getVar('layout');
    	$user = JFactory::getUser();
    	if (isset($user->id))
    		$user_id = $user->id;
    	if (getCurrentUserType($user_id)=='Employer User')
    	switch ($layout)
    	{    		    		
    		
    		case 'jalist':
				switch (checkSearchResumeExpired())
		        {
		        	case 'not':
		        		JError::raiseWarning('',JText::_("You have not subscribed Search resume package, please buy now to get search resume permission"));
		        		$this->setRedirect(JRoute::_('index.php?option='.JBCOMNAME.'&view=japosts&layout=selectplan&tab=2'));
		        		return;
		        	case 'expired':
		        		JError::raiseWarning('',JText::_('Your search resume package is expired'));
		        		$this->setRedirect(JRoute::_('index.php?option='.JBCOMNAME.'&view=japosts&layout=selectplan&tab=2'));
		        		return;
		        	case 'future':
		        		JError::raiseWarning('',JText::_('Your search resume package is not reached yet'));
		        		$this->setRedirect(JRoute::_('index.php?option='.JBCOMNAME.'&view=japosts&layout=selectplan&tab=2'));
		        		return;
		        		
		        }    		
    			
    	}		
			
		parent::display();
    }
    
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
    	$user = JFactory::getUser();
        $Itemid = JRequest::getVar('Itemid');
        if (($user->usertype!='Administrator')&&($user->usertype!='Super Administrator'))
        	$this->setRedirect(JRoute::_("index.php?option=". JBCOMNAME ."&view=". $this->get('fullname')."&layout=jaview&Itemid=".$Itemid));
        else 
        	$this->setRedirect( JRoute::_('index.php?option='. JBCOMNAME .'&view=jacandidates'));
    }
    
    /**
    * Remove a configuration row
    * 
    */
    function remove(){
    	// Check for request forgeries
        if(!JRequest::checkToken()){
            JResponse::setHeader('HTTP/1.0 403',true);
            JError::raiseWarning( 403, JText::_("Access denied. You don't have permission.") ); 
            echo "<a href='javascript:window.history.go(-1);'>". JText::_('Back')."</a>"; 
            return;
        }
        $task = JRequest::getVar('task', 'remove');
        $model =& $this->getModel(JBPREFIX.'resumes');
        $rc = $model->remove($task);
		$Itemid = JRequest::getVar('Itemid');
        $limited = JRequest::getVar('limited', 0);
        
        if($rc){
            $this->setMessage(JText::_('Successfully delete resume!'));
            $this->setRedirect(JRoute::_('index.php?option='. JBCOMNAME .'&view=jajobs&layout=jalist'));
        } else {
            JError::raiseWarning(0, JText::_('Could NOT remove the items'));
            $this->setRedirect(JRoute::_('index.php?option='. JBCOMNAME .'&view='.$this->get('fullname').'&limited='.$limited.'&Itemid='.$Itemid));
        }		
	}
	
	/*function activate(){
    	// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
        $Itemid = JRequest::getVar('Itemid');
        $model =& $this->getModel($this->get('fullname'));
        $task = JRequest::getVar('task', 'approved');
        $model->activate($task);
		$this->setRedirect('index.php?option='. JBCOMNAME .'&view='.$this->get('fullname')."&Itemid=".$Itemid);
	}*/
    
    /**
    * Edit forms
    */
    function edit($postback = false){
    	JRequest::setVar('postback',$postback);
        JRequest::setVar('edit', true);
        JRequest::setVar('layout','jaform');
        parent::display();
    }
    
	/*function processPaypal($paytype,$amount){
        require_once(JPATH_COMPONENT.DS."models".DS."jatransaction.php");
        //$pClass=new Paypal($jbconfig);
        $rowItem->item_name = $paytype;        
        $rowItem->amount = $amount;
        $user = &JFactory::getUser();
        $rowItem->userId = $user->id;
        $pClass = $this->getModel(JBPREFIX.'Paypal');
        $pClass->processPayment1($rowItem);
    }*/
    
	function starred(){
		$Itemid = JRequest::getVar('Itemid');
		if(!$Itemid){
			$Itemid = get_Itemid(array('option'=>'com_jajobboard', 'view'=>'jaresumes', 'layout'=>'jalist'));
		}
		        
        $user = &JFactory::getUser();
        if($user->id){
        	
            $task = JRequest::getVar('task', 'starred');
            $cid = JRequest::getVar('cid');
            $cid = $cid[0];
            JRequest::setVar('cid[]',$cid);
            
            $alias = JRequest::getVar('alias', '');
            
            $model =& $this->getModel('jashortlist' );
 			JRequest::setVar('table_name','ja_resumes');
            $result = $model->starred($task);

            if ($result=='new')
            	$this->setMessage(JText::sprintf(JText::_('Successfully added to My favourite resumes'), $n));
            else 
            	$this->setMessage(JText::sprintf('This resume is already saved', $n));
            
            $this->setRedirect(JRoute::_('index.php?option='. JBCOMNAME .'&view=jaresumes&layout=jaview&cid[]='.$cid . '&Itemid='.$Itemid . '&alias='.$alias)); 
               
        }
    }
	
	
    /**
	* Save configuration record
	*/
	function save(){
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$userdetail = JRequest::getVar('userdetail');
		$user = JFactory::getUser();
		// Check for request forgeries
		$step = JRequest::getVar('step', ''); 
        ($step == 'done') or JRequest::checkToken() or jexit( 'Invalid Token' );                
        
		
		$model	=& $this->getModel( $this->get('fullname') );
        
        $id = $model->store();
        if($id){
        	if ((isset($cid[0]))&&$cid[0]!=0) {
        		$msg = JText::_( 'Successfully updated your resume!' );
        		$this->setRedirect( JRoute::_('index.php?option=com_jajobboard&view=jaresumes&layout=jaview&cid[]='.$id), $msg );
        	} 
        	else{ 
        		$msg = JText::_( 'Successfully created your resume! Please wait for admin approval before you can perform other activities as a job seeker' );
        		$this->setRedirect( JRoute::_('index.php?option=com_jajobboard&view=jajobs&layout=jalist'), $msg );
        	}
        	        
        } else {
			JError::raiseWarning(0, JText::_('Data is incorrect'));
            $this->edit(true);
        }
	}
    
    /**
    * Save selected jobs to shortlist
    * 
    */
    /*function savejobs(){
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $user = &JFactory::getUser();
        if($user->id){
            $task = JRequest::getVar('task', 'savejobs');
            $model =& $this->getModel(JBPREFIX.'ShortList' );
            $model->savejobs($task);

            $this->setMessage(JText::sprintf('Jobs Saved', $n));
            $this->setRedirect('index.php?option='. JBCOMNAME .'&view='.$model->get('fullname'));    
        } else {
            // Redirect to user login
        }
    }*/
    
    function sendfriend(){
        JRequest::setVar('sendfriend', true);
        $cid = JRequest::getVar('cid', array(), 'post', 'array');
        JArrayHelper::toInteger($cid);
        $Itemid = JRequest::getVar('Itemid');
        
        $model = & $this->getModel($this->get('fullname'));
        $result = $model->resumeMailSendFriend($cid[0]);
        if (JRequest::getVar('sendmyself'))
        	$model->resumeMailSendMe($cid[0],'ResumeMailSendMe');
        if ($result)
        {
	        $this->setMessage(JText::_('Successfully sent email to your friend'));
	        $this->setRedirect(JRoute::_('index.php?option='. JBCOMNAME .'&view='.$this->get('fullname').'&layout=jaview&cid[]='.$cid[0].'&Itemid='.$Itemid));    
        }
	    else 
        {	
        	JRequest::setVar('mail_postback',true);
        	JRequest::setVar('layout','jaview');
    		parent::display();
        }
    }
    
    function sendtojobseeker(){
    	
        JRequest::setVar('sendtojobseeker', true);
        $cid = JRequest::getVar('cid', array(), 'post', 'array');
        JArrayHelper::toInteger($cid);
        $Itemid = JRequest::getVar('Itemid');
        
        $model = & $this->getModel($this->get('fullname'));
        $result = $model->sendtojobseeker($cid[0]);
        
        if (JRequest::getVar('mailjobseekersendmyself') && $result)
        {
        	$model->sendtojobseekerme($cid[0],'ResumeMailSendJobseekerMe');
        }
        if ($result)
        {
	        $this->setMessage(JText::_('Successfully sent email to job-seeker'));
	        $this->setRedirect(JRoute::_('index.php?option='. JBCOMNAME .'&view='.$this->get('fullname').'&layout=jaview&cid[]='.$cid[0].'&Itemid='.$Itemid));    
        }
	    else 
        {	
        	JRequest::setVar('mail_postback_jobseeker',true);
        	JRequest::setVar('layout','jaview');
    		parent::display();
        }   
    }
    
   /* function applyjob(){
        JRequest::checkToken() or die('Invalid token');
        $user = &JFactory::getUser();
        if($user->id){
        } else {
            $this->setMessage(JText::_('Please login or register'));
            $this->setRedirect(JRoute::_('index.php?option=com_user&view=login'));
        }
        
    }*/
    
    function download(){
         $model    =& $this->getModel( $this->get('fullname') );    
         // Get current URI
        $uri = JFactory::getURI();
        $url = $uri->toString();
        $url = base64_encode($url);
	    // Check download Permission
	    if( ! $model->download() ){
	       $this->setRedirect(JRoute::_("index.php?option=com_user&view=login&return=".$url), "You must login as an Employer to download document");
	       return ;   
	    }
        
        exit;  
    }
    
    function remove_attach()
    {
    	$model    =& $this->getModel( $this->get('fullname') );
    	$model->remove_attach();
         exit;
    }
    
    function multi_starred()
    {
    	$Itemid = JRequest::getVar('Itemid');
		if(!$Itemid){
			$Itemid = get_Itemid(array('option'=>'com_jajobboard', 'view'=>'jaresumes', 'layout'=>'jalist'));
		}
		
    	$user = &JFactory::getUser();
    	if (!$user->id)
    	{
    		$uri		= JRoute::_('index.php?option=com_jajobboard&view=jaresumes&layout=jalist&Itemid='.$Itemid);
			$return		= $uri->toString();
    		$this->setRedirect(JRoute::_("index.php?option=com_user&view=login&return=".base64_encode($return)),JText::_('You must log in to add favorite resume(s)'));
        	return ;
    	}
    	$cid = JRequest::getVar('cid');
    	$msg = '';
    	if (count($cid)>0)
    	{
	        if($user->id){
	            if (JRequest::getVar('remove_starred'))
	        		$task = 'unstarred';
	        	else 
	        		$task = 'starred';
	            $model =& $this->getModel('jashortlist' );
	            JRequest::setVar('table_name','ja_resumes');
	            $result = $model->multi_starred($task);
	            if (!JRequest::getVar('remove_starred'))
	            {
		            if ($result)
		                $msg = JText::_('Successfully added to my favorite resume');
			        else {
			        	JError::raiseWarning(1,  JText::_('Unsuccessfully added to my favourite resume'));
			        }
		            $this->setRedirect(JRoute::_('index.php?option='. JBCOMNAME .'&view=jaresumes&layout=jalist&Itemid='.$Itemid),$msg);
	            }
	            else 
	            {
	            	if ($result)
		                $msg = JText::_('Successfully removed favorite resume');
	            	else {
			        	JError::raiseWarning(1,  JText::_('Unsuccessfully removed to my favourite resume'));
			        }
		            $this->setRedirect(JRoute::_('index.php?option='. JBCOMNAME .'&view=jaresumes&layout=jaemployerlist&Itemid='.$Itemid),$msg);
		            
	            }
	        }
    	}
    	else 
    	{
    		if (!JRequest::getVar('remove_starred'))
	        {
	    		$msg = JText::_('Please make a selection from the list to save');
	    		$this->setRedirect(JRoute::_('index.php?option='. JBCOMNAME .'&view=jaresumes&layout=jalist&Itemid='.$Itemid),$msg);
	        }
	        else 
	        {
	        	$msg = JText::_('Please make a selection from the list to remove favorite');
	    		$this->setRedirect(JRoute::_('index.php?option='. JBCOMNAME .'&view=jaresumes&layout=jaemployerlist&Itemid='.$Itemid),$msg);
	        }
    	}
    }
    
    function multi_view()
    {
    	$Itemid = JRequest::getVar('Itemid');
		if(!$Itemid){
			$Itemid = get_Itemid(array('option'=>'com_jajobboard', 'view'=>'jaresumes', 'layout'=>'jalist'));
		}
    	$user = &JFactory::getUser();
    	$cid = JRequest::getVar('cid');
    	if (count($cid)>0)
    	{
    		JRequest::setVar('layout','jaview');
    		$this->display();
    	}
    	else 
    	{
    		$msg = JText::_('Please make a selection from the list to view');
    		$this->setRedirect(JRoute::_('index.php?option='. JBCOMNAME .'&view=jaresumes&layout=jalist&Itemid='.$Itemid),$msg);
    	}
    	
    }    
}
?>
